package com.sdhs.system.syncdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdhs.system.syncdata.domain.SyncSysRoleUser;
import com.sdhs.system.syncdata.mapper.SyncSysRoleUserMapper;
import com.sdhs.system.syncdata.service.SyncSysRoleUserService;
import com.sdhs.system.syncdata.util.TextUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 角色用户关联关系(SysRoleUser)表服务实现类
 *
 * @author makejava
 * @since 2022-09-29 11:17:39
 */
@Service("sysRoleUserService")
public class SyncSysRoleUserServiceImpl extends ServiceImpl<SyncSysRoleUserMapper, SyncSysRoleUser> implements SyncSysRoleUserService {
    @Resource
    private SyncSysRoleUserMapper sysRoleUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SyncSysRoleUser queryById(Long id) {
        return this.sysRoleUserDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sysRoleUser 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<SyncSysRoleUser> queryByPage(SyncSysRoleUser sysRoleUser, PageRequest pageRequest) {
        long total = this.sysRoleUserDao.count(sysRoleUser);
        return new PageImpl<>(this.sysRoleUserDao.queryAllByLimit(sysRoleUser, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sysRoleUser 实例对象
     * @return 实例对象
     */
    @Override
    public SyncSysRoleUser insert(SyncSysRoleUser sysRoleUser) {
        this.sysRoleUserDao.insert(sysRoleUser);
        return sysRoleUser;
    }

    /**
     * 修改数据
     *
     * @param sysRoleUser 实例对象
     * @return 实例对象
     */
    @Override
    public SyncSysRoleUser update(SyncSysRoleUser sysRoleUser) {
        this.sysRoleUserDao.update(sysRoleUser);
        return this.queryById(sysRoleUser.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.sysRoleUserDao.deleteById(id) > 0;
    }

    @Override
    public boolean insertBatch(String userId,List<String> roleIds,String creator) {

        if(!TextUtil.isNull(roleIds)){
            List<SyncSysRoleUser> list=new ArrayList<>();
            int size=roleIds.size();
            for(int i=0;i<size;i++){
                SyncSysRoleUser sysRoleUser=new SyncSysRoleUser();
//                String id= TextUtil.getRandom(19);
//                sysRoleUser.setId(Long.parseLong(id.trim()));
                sysRoleUser.setRoleId(Long.parseLong(roleIds.get(i)));
                sysRoleUser.setUserId(Long.parseLong(userId));
                sysRoleUser.setCreator(creator);
                sysRoleUser.setCreateDate(new Date());
                list.add(sysRoleUser);
            }
            boolean flag = this.saveBatch(list);
           return flag;

        }
        return false;
    }

    @Override
    public int deleteRole(String userId) {
        QueryWrapper<SyncSysRoleUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        int delete = sysRoleUserDao.delete(queryWrapper);
        return delete;
    }

    @Override
    public List<Object> getRoles(String uid) {
        QueryWrapper<SyncSysRoleUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",uid);
        queryWrapper.select("id");
        List<Object> objects = sysRoleUserDao.selectObjs(queryWrapper);

        return objects;
    }
}
